package com.boge.modules.sys.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boge.common.utils.R;
import com.boge.common.validator.ValidatorUtils;
import com.boge.common.validator.group.AddGroup;
import com.boge.modules.sys.entity.SysRoleEntity;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.entity.SysUserRoleEntity;
import com.boge.modules.sys.service.SysRoleService;
import com.boge.modules.sys.service.SysUserRoleService;
import com.boge.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/web")
public class WebController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysUserRoleService userRoleService;

    public static final String BASE_URL = "http://localhost:8070/base-fast/file/fileController/download?fileName=";


    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody SysUserEntity user){
        // 绑定角色
        userService.saveUser(user);
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name","商家");
        List<SysRoleEntity> list = roleService.list(wrapper);
        if(list != null && list.size() > 0){
            SysRoleEntity sysRoleEntity = list.get(0);
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setUserId(user.getUserId());
            entity.setRoleId(sysRoleEntity.getRoleId());
            userRoleService.save(entity);
        }
        return R.ok();
    }

    //  导出接口
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception{
        // 从数据库查询出所有的数据
        List<SysUserEntity> list=userService.list();
        //通过 工具类创建writer   写出到磁盘路径
        // 在内存操作，写出到浏览器
        ExcelWriter writer= ExcelUtil.getWriter(true);
        // 自定义标题别名
//       writer.addHeaderAlias("username","用户名");
//       writer.addHeaderAlias("password","密码");
//       writer.addHeaderAlias("nikename","昵称");
//       writer.addHeaderAlias("email","邮箱");
//       writer.addHeaderAlias("phone","电话");
//       writer.addHeaderAlias("address","地址");
//       writer.addHeaderAlias("createTime","创建时间");
//       writer.addHeaderAlias("avatar","头像");
        // 一次性写出list内的对象到Excel，使用默认样式，强制输出标题
        writer.write(list);
        //  设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName= URLEncoder.encode("用户信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename=" +fileName+".xlsx");

        // 把writer里的东西写入到输出流里
        ServletOutputStream out=response.getOutputStream();
        writer.flush(out);
        out.close();
        writer.close();
    }
}
